//
//  ShopDetailViewController.m
//  ITASTRA
//
//  Created by Apple on 09/12/12.
//  Copyright (c) 2012 me. All rights reserved.
//

#import "ShopDetailViewController.h"

@interface ShopDetailViewController ()

@end

@implementation ShopDetailViewController
@synthesize dictShop;
@synthesize mpView,coordinate,stringLat,stringLong;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.mpView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 300, 120)];
    [self.mpView setUserInteractionEnabled:NO];
    self.stringLat = [dictShop objectForKey:@"address_latitude"];
    self.stringLong = [dictShop objectForKey:@"address_longitude"]; 
    [self createMap];

//    tblShop.style = UITableViewStyleGrouped;
    tblShop.backgroundView = nil;
    tblShop.backgroundColor = [UIColor clearColor];
    
    imageShop = [[EGOImageView  alloc] initWithFrame:CGRectMake( 0, 0, 300, 320)];
    imageShop.imageURL = [NSURL  URLWithString:[[dictShop  objectForKey:@"first_image_big"]stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return 44;
    if(indexPath.section == 3)
            return 50;
    if (indexPath.section == 2) {
        return [ModalController  calculateHeightOfTextFromWidth:[dictShop  objectForKey:@"description"] :[UIFont systemFontOfSize:12.0] :320 :UILineBreakModeWordWrap]+20;
    } 
    if (indexPath.section == 1) {
        return 320;
    } 
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    switch (indexPath.section) {
        case 0:
        {
            cell.textLabel.font = [UIFont  boldSystemFontOfSize:18];
            cell.textLabel.textAlignment = UITextAlignmentCenter;
//            cell.textLabel.numberOfLines = 0;
            cell.textLabel.text = [dictShop  objectForKey:@"name"];
        }
            break;
        case 1:
        {
            cell.backgroundColor = [UIColor  clearColor];
            cell.contentView.backgroundColor = [UIColor  clearColor];
            cell.backgroundView.backgroundColor = [UIColor  clearColor];
            [cell.contentView addSubview:imageShop];
        }
            break;
        case 2:
        {
            cell.textLabel.font = [UIFont  systemFontOfSize:12.0];
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.text = [dictShop  objectForKey:@"description"];
        }
            break;
        case 3:
        {
            cell.textLabel.font = [UIFont  boldSystemFontOfSize:12.0];
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.text = [NSString stringWithFormat:@"Address : %@ %@,%@ %@ %@",[dictShop objectForKey:@"address_street_number"],[dictShop objectForKey:@"address_street"],[dictShop objectForKey:@"address_region"],[dictShop objectForKey:@"address_province"],[dictShop objectForKey:@"address_zip"]];
        }
            break;
        case 4:
        {
            [cell.contentView addSubview:self.mpView];
        }
            break;
        default:
            break;
    }
//    cell.textLabel.text = [[arrayDetail  objectAtIndex:indexPath.row] objectForKey:@"name"];
//    cell.accessoryType = 1;
    //    
    //	
    //	switch(indexPath.section)
    //	{
    //		case 0:
    //            cell.textLabel.text = theList.id;
    //			break;
    //		case 1:
    //			cell.textLabel.text = theList.title;
    //			break;
    //		case 2:
    //			cell.textLabel.text = theList.description;
    //			break;
    //        case 3:
    //			cell.textLabel.text = theList.image;
    //			break;
    //        case 4:
    //            cell.textLabel.text = theList.count_straordinari;
    //			break;
    //	}
    //	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    DetailGuideViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detailGuid"];
//    detailView.mainid = [NSString stringWithFormat:@"%d",theList.objID];
//    detailView.catId  = [[arrayDetail  objectAtIndex:indexPath.row] objectForKey:@"id"];
//    
//    //    theList = [app.listArray objectAtIndex:indexPath.row];
//    
//    //    detailView.theList = theList;
//    [self.navigationController pushViewController:detailView animated:YES];
//    
    
}


#pragma mark -mapview-

-(void)createMap
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.05;
    span.longitudeDelta=0.05;
    self.mpView.delegate = self;
    CLLocationCoordinate2D location=self.mpView.userLocation.coordinate;
    
    location.latitude=[self.stringLat floatValue];
    location.longitude=[self.stringLong floatValue];
    region.span=span;
    region.center=location;
    
    MyAnnotation *addAnnotation = [[MyAnnotation alloc] init]  ;
    [addAnnotation setCoordinate:location];
    
    /*Geocoder Stuff*/
    [self.mpView setRegion:region animated:TRUE];
    [self.mpView regionThatFits:region];
    
    [self.mpView addAnnotation:addAnnotation];
}

- (MKAnnotationView*)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSString *string   =   @"hello";
    
	MKPinAnnotationView *annView = nil;
    
    if (annotation == mapview.userLocation) 
    {
        return nil;
    }
    
    
	annView = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:string];
	MyAnnotation *annot = (MyAnnotation*)annotation;
	// If we have to, create a new view
	if(annView == nil)
    {
        // //////NSLog(@"here");
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annot reuseIdentifier:string];
        annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        
        //annView.tag = annot.ann_tag;
    }
    //MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:string];  
	
	annView.canShowCallout = YES;  
	
	//[annView setSelected:YES];  	
	
    //annView.rightCalloutAccessoryView = 
	[annView setPinColor:MKPinAnnotationColorGreen];
	
	annView.calloutOffset = CGPointMake(-5, 5);
	annView.animatesDrop=NO; 
	return annView;
}


@end
